package com.example.project_springboot.controller;

import com.example.project_springboot.pojo.Emp;
import com.example.project_springboot.pojo.Result;
import com.example.project_springboot.service.EmpService;
import com.example.project_springboot.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@Slf4j
public class LoginController {
    @Autowired
    EmpService empService;
    @PostMapping("/login")
    public Result login(@RequestBody Emp emp) {
        Emp emp1 = empService.login(emp);
        if (emp1 != null) {
            HashMap<String, Object> claims = new HashMap();
            claims.put("id", emp1.getId());
            claims.put("name", emp1.getName());
            claims.put("username", emp1.getUsername());
            String s = JwtUtils.generateJwt(claims);
            return Result.success(s);
        }
        return Result.error("账号密码错误");
    }
}
